package com.itheima.d3_collection_test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
//斗地主

public class Room {
    private List<Card> allCards =new ArrayList<>();

    public Room(){
        String[] numbers ={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] colors ={"♠","♥","♣","♦"};

        int size = 0;

        for (String number : numbers) {
            size++;
            for (String color : colors) {
                Card c =new Card(number,color,size);
                allCards.add(c);
            }
        }

        Card c1 =new Card("","joker",++size);
        Card c2 =new Card("","Joker",++size);
        Collections.addAll(allCards,c1,c2);
        System.out.println("新牌:"+allCards);
    }

    public void start() {
        Collections.shuffle(allCards);
        System.out.println("洗牌后"+allCards);

        List<Card> lin =new ArrayList<>();
        List<Card> wang =new ArrayList<>();
        List<Card> yang =new ArrayList<>();

        for (int i = 0; i < allCards.size()-3; i++) {
            Card c= allCards.get(i);
            if(i%3 == 0){
                lin.add(c);
            }else if(i%3 ==1){
                wang.add(c);
            }else if(i%3 ==2){
                yang.add(c);
            }
        }

        sortCards(lin);
        sortCards(wang);
        sortCards(yang);

        System.out.println("阿林:"+lin);
        System.out.println("阿王:"+wang);
        System.out.println("阿杨:"+yang);
        List<Card> cards = allCards.subList(allCards.size() - 3, allCards.size());
        System.out.println("底牌为"+cards);

        wang.addAll(cards);
        sortCards(wang);
        System.out.println("老王抢到地主后:"+wang);
    }

    private void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o1.getSize()- o2.getSize();
            }
        });
    }
}
